package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.rocketmqconfig.OrderDelayMsgProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 利用mq发送延迟取消订单消息
 **/
@Component
@Slf4j
public class SendMessageHandler extends AbstractTransHandler {

    @Autowired
    OrderDelayMsgProducer producer;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        // 向下转型
        CreateOrderContext createOrderContext = (CreateOrderContext) context;

        // 发送超时取消订单的消息
        String orderId = createOrderContext.getOrderId();

        boolean sendMsg = producer.sendOrderCancelMsg(orderId);

        if (!sendMsg) {
            throw new BizException(OrderRetCode.ORDER_DELAY_MSG_SEND_FAIL.getCode(),
                    OrderRetCode.ORDER_DELAY_MSG_SEND_FAIL.getMessage());
        }
        return true;
    }
}